#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 5
#define N 1000
#define MEGEXTRA 1000000
pthread_attr_t attr;
void * DoWork(void * arg)
{
size_t iStackSize;
pthread_attr_getstacksize(&attr, &iStackSize);
printf("Thread %d, stack size = %d bytes\n", (int)arg, iStackSize);
double A[N][N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
A[i][j] = ((i*j)/3.452) + (N-i);
pthread_exit(NULL);
}
int main()
{
int iRet;
int i;
pthread_attr_init(&attr);
size_t iStackSize;
pthread_attr_getstacksize(&attr, &iStackSize);
printf("Default stack size = %d\n", iStackSize);
iStackSize = sizeof(double)*N*N + MEGEXTRA;
printf("New stack size = %d\n", iStackSize);
pthread_attr_setstacksize(&attr, iStackSize);
pthread_t threads[NUM_THREADS];
for (i = 0; i < NUM_THREADS; i++)
{
iRet = pthread_create(&threads[i], &attr, DoWork, (void*)i);
if(iRet != 0) {
printf("Error: pthread_create failed = %d\n", iRet);
exit(-1);
}
}
printf("Created %d threads.\n", i);
pthread_exit(NULL);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 5
void * PrintHello(void * arg)
{
int iPid = (int)arg;
printf("Hello World! It's me, thread %d\n", iPid);
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++)
{
printf("In main: creating thead %d\n", i);
int iRet = pthread_create(&threads[i], NULL, PrintHello, (void*)i);
if(iRet != 0) {
printf("Error: pthread_create failed = %d\n", iRet);
exit(-1);
}
}
pthread_exit(NULL);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
//if this two num is too small, even you comment the pthread_mutex_lock
//and pthread_mutex_unlock, you may be can't see the difference.
#define NUM_THREADS 80
#define VECLEN 100000
typedef struct
{
double * a;
double * b;
double sum;
int veclen;
}DOTDATA;
DOTDATA g_dotstr;
pthread_t threads[NUM_THREADS];
pthread_mutex_t mutex;
void * dotprod(void * arg)
{
double * x = g_dotstr.a;
double * y = g_dotstr.b;
int iOffset = (int)arg;
int iLen = g_dotstr.veclen;
int iStart = iOffset * iLen;
int iEnd = iStart + iLen;
for (int i = iStart; i < iEnd; i++)
{
pthread_mutex_lock(&mutex);
g_dotstr.sum += (x[i] * y[i]);
pthread_mutex_unlock(&mutex);
}
pthread_exit((void*)0);
}
int main()
{
int iRet;
int i;
double * a = (double*)malloc(NUM_THREADS * VECLEN * sizeof(double));
double * b = (double*)malloc(NUM_THREADS * VECLEN * sizeof(double));
for (i = 0; i < NUM_THREADS * VECLEN; i++)
{
a[i] = 1.0;
b[i] = a[i];
}
g_dotstr.a = a;
g_dotstr.b = b;
g_dotstr.sum = 0.0;
g_dotstr.veclen = VECLEN;
pthread_mutex_init(&mutex, NULL);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (i = 0; i < NUM_THREADS; i++)
{
iRet = pthread_create(&threads[i], &attr, dotprod, (void*)i);
if(iRet != 0) {
printf("Error: pthread_create failed = %d\n", iRet);
exit(-1);
}
}
pthread_attr_destroy(&attr);
for (i = 0; i < NUM_THREADS; i++)
{
void *status = NULL;
iRet = pthread_join(threads[i], &status);
if(iRet != 0) {
printf("Error: pthread_join failed = %d\n", iRet);
exit(-1);
}
}
printf("In main: program completed. Sum = %lf.\n", g_dotstr.sum);
pthread_mutex_destroy(&mutex);
pthread_exit(NULL);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <math.h>
#define NUM_THREADS 5
void * BusyWork(void * arg)
{
printf("Thread %d starting ...\n", (int)arg);
double result = 0.0;
for (int i = 0; i < 1000000; i++)
{
result = result + sin(i) * tan(i);
}
printf("Thread %d done, result = %e\n", (int)arg, result);
pthread_exit(arg);
}
int main()
{
int iRet;
int i;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_t threads[NUM_THREADS];
for (i = 0; i < NUM_THREADS; i++)
{
printf("In main: creating thead %d\n", i);
iRet = pthread_create(&threads[i], &attr, BusyWork, (void*)i);
if(iRet != 0) {
printf("Error: pthread_create failed = %d\n", iRet);
exit(-1);
}
}
pthread_attr_destroy(&attr);
for (i = 0; i < NUM_THREADS; i++)
{
void *status = NULL;
iRet = pthread_join(threads[i], &status);
if(iRet != 0) {
printf("Error: pthread_join failed = %d\n", iRet);
exit(-1);
}
printf("In main: completed join with thread %d status %d\n", i, (int)status);
}
printf("In main: program completed. Exiting.\n");
pthread_exit(NULL);
return 0;
}
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 3
#define TCOUNT 10
#define COUNT_LIMIT 12
int count = 0;
pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;
void * inc_count(void * arg)
{
for (int i = 0; i < TCOUNT; i++)
{
pthread_mutex_lock(&count_mutex);
count++;
if(count == COUNT_LIMIT)
{
pthread_cond_signal(&count_threshold_cv);
printf("inc_count(): thread %d count = %d, thresold reached.\n", (int)arg, count);
}
printf("inc_count(): thread %d count = %d\n", (int)arg, count);
pthread_mutex_unlock(&count_mutex);
sleep(1);
}
pthread_exit(NULL);
}
void * watch_count(void * arg)
{
printf("Starting watch_count(): thread %d\n", (int)arg);
pthread_mutex_lock(&count_mutex);
while (count < COUNT_LIMIT) {
pthread_cond_wait(&count_threshold_cv, &count_mutex);
printf("watch_count(): thread %d condition signal received.\n", (int)arg);
count += 125;
printf("watch_count(): thread %d count now = %d\n", (int)arg, count);
}
pthread_mutex_unlock(&count_mutex);
pthread_exit(NULL);
}
int main()
{
int iRet;
int i;
pthread_t threads[NUM_THREADS];
pthread_mutex_init(&count_mutex, NULL);
pthread_cond_init(&count_threshold_cv, NULL);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&threads[0], &attr, watch_count, (void*)0);
pthread_create(&threads[1], &attr, inc_count, (void*)1);
pthread_create(&threads[2], &attr, inc_count, (void*)2);
for (i = 0; i < NUM_THREADS; i++)
{
iRet = pthread_join(threads[i], NULL);
if(iRet != 0) {
printf("Error: pthread_join failed = %d\n", iRet);
exit(-1);
}
}
printf("In main: program completed. Exiting.\n");
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&count_mutex);
pthread_cond_destroy(&count_threshold_cv);
pthread_exit(NULL);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 2
struct thread_data {
int thread_id;
int sum;
const char * message;
};
struct thread_data thread_data_array[NUM_THREADS] = {
{1, 1, "thread 1"},
{2, 2, "thread 2"}
};
void * PrintHello(void * arg)
{
struct thread_data * pData = (struct thread_data*)arg;
printf("Hello World! It's me, %d:%s\n", pData->thread_id, pData->message);
pthread_exit(NULL);
}
int main()
{
pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++)
{
printf("In main: creating thead %d\n", i);
int iRet = pthread_create(&threads[i], NULL, PrintHello, (void*)&thread_data_array[i]);
if(iRet != 0) {
printf("Error: pthread_create failed = %d\n", iRet);
exit(-1);
}
}
pthread_exit(NULL);
return 0;
}