Semaphore and threads in C

A thread is a basic unit of CPU utilization.

A thread shares with its peer threads its:

-code section

-data section

-operating-system resources

so take care when deal with global variable(can add mutext

lock or wait semaphore)

Synchronization:

http://www.csc.villanova.edu/~mdamian/threads/posixsem.html

can use POSIX semaphores:

      int sem_init(sem_t *sem, int pshared, unsigned int value);
  • sem points to a semaphore object to initialize
  • pshared is a flag indicating whether or not the semaphore should
      be shared with fork()ed processes. LinuxThreads does not currently
      support shared semaphores
  • value is an initial value to set the semaphore to

To wait on a semaphore, use sem_wait:

      int sem_wait(sem_t *sem);

Example of use:

      sem_wait(&sem_name);
  • If the value of the semaphore is negative, the calling process blocks; one of the blocked
    processes wakes up when another process calls sem_post.



To increment the value of a semaphore, use sem_post:

      int sem_post(sem_t *sem);

Example of use:

      sem_post(&sem_name);
  • It increments the value of the semaphore and wakes up a blocked process waiting on the
    semaphore, if any.

notice that threads can work concurrently,and the speed you

don‘t know,so sometimes the value change is not as you think,so take care

(lab1.3,e0,e1,changing values of e0 and e1 first then sem_post(sem1)).

When the program stops somewhere, probably it‘s waiting a signal but hasn‘t arrived.

check carefully,may have deadlock.

Thread:

pthread_detach(pthread_self());//parent is not joining

use this for all threads created,the main_thread doesn‘t need to use pthread_join

because the resources occupied by these threads can be released automatically

after exiting.

Can‘t use kill() or signal() for threads,because they are for processes(like children

and parents),not for threads.

threads creation:

status=pthread_create(&th_a,NULL,read_thread1,&one);
if(status!=0)
printf("error to create thread 1");

时间: 2024-10-08 17:06:08

Semaphore and threads in C的相关文章

Linux IPC (Semaphore)

/* * This demo shows how to use semaphore between threads. * */ #include <pthread.h>#include <semaphore.h>#include <sys/types.h>#include <stdio.h>#include <unistd.h> /* * Global var */int number;sem_t sem_id; void* thread_one

指定Python线程数目

可以通过threading.semaphore()来指定并行运行的Python线程的数目. #!/usr/bin/python2.7 #File: threadsNum.py #Author: lxw #Time: 2014-09-07 #Usage: Demonstration for control the number of threads. import threading from myThread import MyThread from time import sleep def

Linux IPC(mutex &amp; cond)

/* * This demo shows how to use semaphore between threads. * */ #include <pthread.h>#include <semaphore.h>#include <sys/types.h>#include <stdio.h>#include <unistd.h> /* * Global shared resource */struct shared_resouce {    in

线程系列08,实现线程锁的各种方式,使用lock,Montor,Mutex,Semaphore以及线程死锁

当涉及到多线程共享数据,需要数据同步的时候,就可以考虑使用线程锁了.本篇体验线程锁的各种用法以及线程死锁.主要包括: ※ 使用lock处理数据同步※ 使用Monitor.Enter和Monitor.Exit处理数据同步※ 使用Mutex处理进程间数据同步※ 使用Semaphore处理数据同步※ 线程死锁 □ 使用lock处理数据同步 假设有一个类,主要用来计算该类2个字段的商,在计算商的方法之内让被除数自减,即被除数有可能为零.使用lock语句块保证每次只有一个线程进入该方法. class Th

Semaphore

Before obtaining an item each thread must acquire a permit from the semaphore, guaranteeing that an item is available for use. When the thread has finished with the item it is returned back to the pool and a permit is returned to the semaphore, allow

C#各种同步方法 lock, Monitor,Mutex, Semaphore, Interlocked, ReaderWriterLock,AutoResetEvent, ManualResetEvent

看下组织结构: System.Object System.MarshalByRefObject System.Threading.WaitHandle System.Threading.Mutex System.Threading.Semaphore System.Threading.EventWaitHandle System.Threading.ManualResetEvent System.Threading.AutoResetEvent System.Object System.Thre

Java多线程20:多线程下的其他组件之CountDownLatch、Semaphore、Exchanger

前言 在多线程环境下,JDK给开发者提供了许多的组件供用户使用(主要在java.util.concurrent下),使得用户不需要再去关心在具体场景下要如何写出同时兼顾线程安全性与高效率的代码.之前讲过的线程池.BlockingQueue都是在java.util.concurrent下的组件,Timer虽然不在java.util.concurrent下,但也算是.后两篇文章将以例子的形式简单讲解一些多线程下其他组件的使用,不需要多深刻的理解,知道每个组件大致什么作用就行. 本文主要讲解的是Cou

Java多线程之JUC包:Semaphore源码学习笔记

若有不正之处请多多谅解,并欢迎批评指正. 请尊重作者劳动成果,转载请标明原文链接: http://www.cnblogs.com/go2sea/p/5625536.html Semaphore是JUC包提供的一个共享锁,一般称之为信号量. Semaphore通过自定义的同步器维护了一个或多个共享资源,线程通过调用acquire获取共享资源,通过调用release释放. 源代码: /* * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to lic

采用java信号量(semaphore)让线程轮流打印

semaphore是java.util.concurrent包下的并发工具类. A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each acquire() blocks if necessary until a permit is available, and then takes it. Each release() adds a permit, potentially releasing