New Posts  All Forums:Forum Nav:

[C++] Questions - Page 6

post #51 of 91
Thread Starter 
Lol, my exam is in the spring.

Mental system; module ended before Christmas, this project is due next week, and then the exam isn't until April or May.
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #52 of 91
Ok, try the following and tell us what errors you get.

Code:
DWORD WINAPI worker(LPVOID Input)
{
  //ThreadData Data;
 
  //Data = *((ThreadData*)Input);
  //cout << "Thread #" << Data.ThreadID << "." << endl;
  //Data.seconds = 1;
 
  int seconds;
  seconds = ((ThreadData *) Input)->seconds;
 
  cout << "Running for seconds: " << seconds << endl;
  clock_t endwait;
  endwait = clock() + seconds*CLOCKS_PER_SEC;
  while (clock() < endwait)
  {
    cout << "Hello";
  }
 
  cout << "Closing thread..." << endl;
}
post #53 of 91
BlackBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 3.6ghz @ 1.35V Gigabyte X38-DS4 ATI 5870 4GB Corsair Dominator 1033Mhz 
Hard DriveOSMonitorKeyboard
Hitachi P7K500 320GB 64-bit Gentoo Linux / 64-bit Vista Ultimate Sceptre X24-WG 24" WUXGA Deck 82-key 
PowerCaseMouse
Corsair 750TX BlackBox (scratch built) Logitech G5 
  hide details  
Reply
BlackBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 3.6ghz @ 1.35V Gigabyte X38-DS4 ATI 5870 4GB Corsair Dominator 1033Mhz 
Hard DriveOSMonitorKeyboard
Hitachi P7K500 320GB 64-bit Gentoo Linux / 64-bit Vista Ultimate Sceptre X24-WG 24" WUXGA Deck 82-key 
PowerCaseMouse
Corsair 750TX BlackBox (scratch built) Logitech G5 
  hide details  
Reply
post #54 of 91
I haven't done C++ in a long time but can you do the following these days?

cin >> threadchoice;
...
ThreadData Input[threadchoice];


Is this how dynamic allocation is done these days or is this the reason that it's not working? The way I used to do it is like this:

cin >> threadchoice;
...
ThreadData* Input = new ThreadData[threadchoice];

Try changing the code around and see if that works.

Edit: Also, in the first BankID == 0 if else you never initialize the "seconds" data member. Maybe that's why it's printing out some random value.
Edited by mrwalker - 1/24/11 at 4:36pm
post #55 of 91
Thread Starter 
Quote:
Originally Posted by mrwalker View Post
Edit: Also, in the first BankID == 0 if else you never initialize the "seconds" data member. Maybe that's why it's printing out some random value.
Yup, caught that this morning.

However, while it works fine when I've got one thread, when I use more than one, I get an output like this:

Code:
Running for seconds: Running for seconds: 2293576
Closing thread...
1
Closing thread...
One of the values are initialised, while the other isn't. And also, both threads are interrupting each other
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #56 of 91
Try to allocate the memory dynamically like I suggested with the new operator.

So instead of this:

ThreadData Input[threadchoice];

Now you have:

ThreadData* Input = new ThreadData[threadchoice];

After you allocate the memory like this you use it the same way as you would a regular array.

And also you will have to add a "delete [] Input;" at the end of your program to free the memory you allocated.
post #57 of 91
Thread Starter 
That's worked out quite nicely; all threads have the correct information passing through.

Next question, how do I get the main function to pause while the threads for the function is running? This is the code I'm using:

WaitForMultipleObjects(threadchoice, threadH, true, INFINITE);

I could just use Sleep() for the amount of time the threads will be running for, but is there a way to make the above code work?
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #58 of 91
I was about to talk to you about that. In your code you close the threads before you have waited them out. So to fix that you need to do the following:

Change the following code:
Code:
  for (int i = 0; i < threadchoice; i++)
    CloseHandle(threadH[i]);
  WaitForMultipleObjects(threadchoice, threadH, true, INFINITE);
To this:
Code:
WaitForMultipleObjects(threadchoice, threadH, true, INFINITE);
for (int i = 0; i < threadchoice; i++)
  CloseHandle(threadH[i]);

Edit:
I forgot to mention, if you want a little wait time after your worker function prints a message on the screen then you can add sleep(1); to your worker function after you print the message. That way you will get to read what the threads are doing, otherwise everything happens too fast.
Edited by mrwalker - 8/11/11 at 8:55pm
post #59 of 91
Thread Starter 
Lol, my mind is in reverse. I'll go fix that now.

As for adding Sleep(1)... I need to run another function within the WHILE loop in the worker function, and see how many times it can run in (for example) 4 seconds on 3 threads, and then how many in 4 seconds on 4 threads, etc. So I'll probably just not have it output anything to the screen.
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #60 of 91
This thread has made me feel bad about myself, but has been a learning experience and I now have much reading to do
It plays TF2
(18 items)
 
LG Nexus 5
(12 items)
 
Acer 5745PG
(6 items)
 
CPUMotherboardGraphicsRAM
4790k ASRock Z97 Extreme6 Asus GTX980 Strix 4GB Kingston HyperX Fury Black 2x8GB 
Hard DriveHard DriveHard DriveOptical Drive
OCZ Vector 128GB Seagate 2TB  Seagate 500GB 7200.12 Some LG that works properly 
CoolingOSMonitorKeyboard
Corsair H105 Windows 8 Pro 64bit Samsung B2330h MS Digital Media Keyboard 
PowerCaseMouseMouse Pad
CoolerMaster Silent Pro M600 Coolermaster CM690II Advanced Logitech G500s CyberSnipa Something 
AudioAudio
JVC RX700 Creative X-Fi Titanium 
CPUMotherboardGraphicsRAM
Quad-core 2.3 GHz Krait 400 LG Something + Qualcomm MSM8974 Snapdragon 800 Adreno 330 2 GB 
Hard DriveOptical DriveCoolingOS
32 GB internal storage 8MP AutoAwesomeHDRFace + 1MP SelfieMachine Air Android 4.4.2 
MonitorPowerCaseMouse
True HD IPS+ 1080 x 1920 pixels, 4.95 inches Non-removable Li-Po 2300 mAh battery LG Sexybox Capacitive touchscreen 
CPUGraphicsRAMHard Drive
i5 480m @ 2.66GHz GT420M 1GB 4GB DDR3 WD 750GB 
OSMonitor
Windows 7 Professional 64 bit 15" 1366x768 + two point touchscreen 
  hide details  
Reply
It plays TF2
(18 items)
 
LG Nexus 5
(12 items)
 
Acer 5745PG
(6 items)
 
CPUMotherboardGraphicsRAM
4790k ASRock Z97 Extreme6 Asus GTX980 Strix 4GB Kingston HyperX Fury Black 2x8GB 
Hard DriveHard DriveHard DriveOptical Drive
OCZ Vector 128GB Seagate 2TB  Seagate 500GB 7200.12 Some LG that works properly 
CoolingOSMonitorKeyboard
Corsair H105 Windows 8 Pro 64bit Samsung B2330h MS Digital Media Keyboard 
PowerCaseMouseMouse Pad
CoolerMaster Silent Pro M600 Coolermaster CM690II Advanced Logitech G500s CyberSnipa Something 
AudioAudio
JVC RX700 Creative X-Fi Titanium 
CPUMotherboardGraphicsRAM
Quad-core 2.3 GHz Krait 400 LG Something + Qualcomm MSM8974 Snapdragon 800 Adreno 330 2 GB 
Hard DriveOptical DriveCoolingOS
32 GB internal storage 8MP AutoAwesomeHDRFace + 1MP SelfieMachine Air Android 4.4.2 
MonitorPowerCaseMouse
True HD IPS+ 1080 x 1920 pixels, 4.95 inches Non-removable Li-Po 2300 mAh battery LG Sexybox Capacitive touchscreen 
CPUGraphicsRAMHard Drive
i5 480m @ 2.66GHz GT420M 1GB 4GB DDR3 WD 750GB 
OSMonitor
Windows 7 Professional 64 bit 15" 1366x768 + two point touchscreen 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming