#include <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
pthread_t ntid;
void printids(const void *t)
{
char *s = (char *)t;
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s pid %u tid %u (0x%x)\n", s, (unsigned int)pid,
(unsigned int)tid, (unsigned int)tid);
}
void *thr_fn(void *arg)
{
printids(arg);
return NULL;
}
int main(void)
{
int err;
err = pthread_create(&ntid, NULL, thr_fn, (void *)"Child Process:");
if (err != 0) {
fprintf(stderr, "can't create thread: %s\n", strerror(err));
exit(1);
}
printids("main thread:");
sleep(1);
return 0;
}
g++ thread.cpp -o thread -lpthread ./thread main thread: pid 21046 tid 3612727104 (0xd755d740) Child Process: pid 21046 tid 3604444928 (0xd6d77700)
#include <pthread.h> void pthread_exit(void *value_ptr);
#include <pthread.h> int pthread_join(pthread_t thread, void **value_ptr);
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void* thread_function_1(void *arg)
{
printf("thread 1 running\n");
return (void *)1;
}
void* thread_function_2(void *arg)
{
printf("thread 2 exiting\n");
pthread_exit((void *) 2);
}
void* thread_function_3(void* arg)
{
while (1) {
printf("thread 3 writeing\n");
sleep(1);
}
}
int main(void)
{
pthread_t tid;
void *tret;
pthread_create(&tid, NULL, thread_function_1, NULL);
pthread_join(tid, &tret);
printf("thread 1 exit code %d\n", *((int*) (&tret)));
pthread_create(&tid, NULL, thread_function_2, NULL);
pthread_join(tid, &tret);
printf("thread 2 exit code %d\n", *((int*) (&tret)));
pthread_create(&tid, NULL, thread_function_3, NULL);
sleep(3);
pthread_cancel(tid);
pthread_join(tid, &tret);
printf("thread 3 exit code %d\n", *((int*) (&tret)));
return 0;
}
thread 1 running thread 1 exit code 1 thread 2 exiting thread 2 exit code 2 thread 3 writeing thread 3 writeing thread 3 writeing thread 3 exit code -1
#define PTHREAD_CANCELED ((void *) -1)
#include <pthread.h> int pthread_mutex_destory(pthread_mutex_t *mutex); int pthread_mutex_int(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); pthread_mutex_t mutex = PTHEAD_MUTEX_INITIALIZER;
#include <pthread.h> int pthread_mutex_lock(pthread_mutex_t *mutex); int pthread_mutex_trylock(pthread_mutex_t *mutex); int pthread_mutex_unlock(pthread_mutex_t *mutex);
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NLOOP 5000
int counter;
pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;
void *do_add_process(void *vptr)
{
int i, val;
for (i = 0; i < NLOOP; i ++) {
pthread_mutex_lock(&counter_mutex);
val = counter;
printf("%x:%d\n", (unsigned int)pthread_self(), val + 1);
counter = val + 1;
pthread_mutex_unlock(&counter_mutex);
}
return NULL;
}
int main()
{
pthread_t tida, tidb;
pthread_create(&tida, NULL, do_add_process, NULL);
pthread_create(&tidb, NULL, do_add_process, NULL);
pthread_join(tida, NULL);
pthread_join(tidb, NULL);
return 0;
}
#include <pthread.h> int pthread_cond_destory(pthread_cond_t *cond); int pthread_cond_init(pthead_cond_t *cond, const pthread_condattr_t *attr); pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
#include <pthread.h> int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime); int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); int pthread_cond_broadcast(pthread_cond_t *cond); int pthread_cond_signal(pthread_cond_t *cond);
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
struct msg {
struct msg *next;
int num;
};
struct msg *head;
pthread_cond_t has_product = PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void* consumer(void *p)
{
struct msg *mp;
for(;;) {
pthread_mutex_lock(&lock);
while (head == NULL) {
pthread_cond_wait(&has_product, &lock);
}
mp = head;
head = mp->next;
pthread_mutex_unlock(&lock);
printf("Consume %d\n", mp->num);
free(mp);
sleep(rand() % 5);
}
}
void* producer(void *p)
{
struct msg *mp;
for(;;) {
mp = (struct msg *)malloc(sizeof(*mp));
pthread_mutex_lock(&lock);
mp->next = head;
mp->num = rand() % 1000;
head = mp;
printf("Product %d\n", mp->num);
pthread_mutex_unlock(&lock);
pthread_cond_signal(&has_product);
sleep(rand() % 5);
}
}
int main()
{
pthread_t pid, cid;
srand(time(NULL));
pthread_create(&pid, NULL, producer, NULL);
pthread_create(&cid, NULL, consumer, NULL);
pthread_join(pid, NULL);
pthread_join(cid, NULL);
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有