Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Thread observations

linked list

  • Please log in to reply
No replies to this topic

#1 OnyxDragun

OnyxDragun

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 09 October 2011 - 07:55 AM

I'm working on a school assignment and trying to figure out if my threads are working properly.

Because we're doing Win32 programming and I am using a Mac, I noticed that the BootCamp Partition for Win7 is allocated 1core of my CPU, though I cannot really tell if my threads are working properly. If both cores of my i5 are being used (and Windows just reports 1 core) then Windows is allocating a thread per core so that could be the issue. Anyway... the general idea of what I am trying to do is below.

//Main.c
...
hThread[0] = CreateThread(NULL, 0, OddThread, NULL, 0 &id);
hThread[1] = CreateThread(NULL, 0, EvenThread, NULL, 0, &id2);
Sleep(10);
dwEvent = WaitForMultipleObjects(2, hThreads, FALSE, INFINITE);
...
printList(); // prints the contents of a Singly LinkedList

Now OddThread and EvenThread are pretty much the same, one counts from 0 to 20 via odd numbers, the other in even numbers and places it in a LinkedList.
//threads.c
DWORD WINAPI OffThread(PVOID pVoid)
{
   int i;
   for(i = 1; i < 20; i+=2)
      insertData(&head, i);
   return 0;
}

What I am noticing is that if I set Sleep() in main.c to 0, then the 2nd thread seems to run first and completes whereas the 1st thread doesnt get a chance and thus only the Even numbers are added to the linked list. If I set Sleep() to like 200, then both threads seem to have enough time to fill the LinkedList.

Is this due to the speed of the CPU? Is there a way I can look at testing this better?

In my LinkedList.c file, I do make use of EnterCriticalSection and LeaveCriticalSection to ensure each thread doesn't try and insert a NULL value at the head of the LinkedList.
  • 0





Also tagged with one or more of these keywords: linked list

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download