All Forums |
Register |
Login |
Search |
Subscriptions |
My Profile |
Inbox |
Tool Warehouse |
FAQs |
Resources |
Help |
Member List |
Address Book |
Logout |
|
|
Multi-threaded applications not working right
|
Logged in as: Guest |
Users viewing this topic: none |
|
Login |
|
|
Multi-threaded applications not working right - Oct. 2, '02, 10:51:08 AM
|
|
|
sharon
Posts: 11
Joined: Sep. 9, '02,
Status: offline
|
I have been tasked with porting our application from a Solaris environment to Interix. Most of the port has gone very well except for the couple of progams that are threaded. I recently upgraded to SFU 3 and have downloaded the pthread libraray from the tools section. However, my test programs that I wrote show no concurrency when running. It appears that one thread must end before the next one starts. This is also what I am seeing in our application. Is there something I am doing wrong or does this not work the way I expect it to?
Following is my test program and its output. Thanks for any clues you can give me to correct this.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
#include <pthread.h>
#include <sys/msg.h>
#include <sys/ipc.h>
void *doonething(void *);
void *doanotherthing(void *);
void dowrapup(int, int);
typedef struct {
long mtype;
int mtext;
} mymsg;
int msqid;
int r1=0, r2=0;
main()
{
pthread_t thread1, thread2;
msqid = msgget(42, (IPC_CREAT | 0666));
pthread_create(&thread1, NULL, &doonething, &r1);
pthread_create(&thread2,NULL, &doanotherthing,&r2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
dowrapup(r1, r2);
}
void *doonething(void *times)
{
int i, j;
int *my_times;
mymsg *msg = (mymsg*)malloc(sizeof(mymsg));
my_times = (int *)times;
for (i=0; i<4; i++)
{
msg->mtype = 1012;
msg->mtext = rand();
errno = 0;
fprintf(stderr," Producing random number: %d\n" , msg->mtext);
j = msgsnd(msqid, msg, sizeof(int), 0);
sleep(4);
(*my_times)++;
}
}
void *doanotherthing(void *times)
{
int i, j;
int *my_times;
mymsg *msg = (mymsg*)malloc(sizeof(mymsg));
my_times = (int *)times;
for (i=0; i<4; i++)
{
printf(" Waiting for a message.\n" );
j = msgrcv(msqid, msg, sizeof(int), 1012, 0);
fprintf(stderr," Consuming number: %d\n" ,msg->mtext);
sleep(5);
(*my_times)++;
}
}
void dowrapup(int one_times, int another_times)
{
int total;
total = one_times + another_times;
printf(" wrap up: onething %d, another %d, total %d\n" ,
one_times, another_times, total);
}
Output from program:
Producing random number: 1103527590
Producing random number: 377401575
Producing random number: 662824084
Producing random number: 1147902781
Waiting for a message.
Consuming number: 1103527590
Waiting for a message.
Consuming number: 377401575
Waiting for a message.
Consuming number: 662824084
Waiting for a message.
Consuming number: 1147902781
wrap up: onething 4, another 4, total 8
|
|
|
RE: Multi-threaded applications not working right - Oct. 7, '02, 12:27:19 PM
|
|
|
Rodney
Posts: 3728
Joined: Jul. 9, '02,
From: /Tools lab
Status: offline
|
The current state of the GNU ' pth' (pthreads) library is that it is a non-preemptive threads package. This means that a running thread will continue running until either it exits, calls a pthread API or explicitly gives up control. This infomation is in the ' pth' documentation, but it isn' t made really obvious.
So, weird as it sounds, your program is behaving correctly.
There is in the ' pth' package an API called ' pth_yield()' which works in similar to the pthreads standard ' sched_yield()' . This might help you a bit.
There' s another pthreads package I know of that does do pre-emptive user space threads. That' s the FSU-threads package. I haven' t done anything with the harder parts of it yet myself.
As an FYI, the SFU dev-group is developing pthreads in the kernel space for a future release. I don' t know the release schedule for it.
- Rodney
|
|
|
New Messages |
No New Messages |
Hot Topic w/ New Messages |
Hot Topic w/o New Messages |
|
Locked w/ New Messages |
Locked w/o New Messages |
|
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts |
|
|
|